using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;

namespace OnlineAnadolu
{
    public partial class UrunEkleme : System.Web.UI.Page
    {
        int c = 0;
        int d = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {

            ListBox lb = new ListBox();
           
                string query = "Select categoryname,categoryid from categories where parentid is NULL";
                Database db = new Database();
                DataTable dt = db.select(query);

               
                lb.ID = "0";

                lb.DataTextField = "categoryname";
                lb.DataValueField = "categoryid";
                lb.DataSource = dt;
                lb.DataBind();
                lb.AutoPostBack = true;
                Panel1.Controls.Add(lb);
                ((ListBox)Panel1.FindControl(lb.ID)).SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
          
                //System.Web.UI.WebControls.ListBox lb = new System.Web.UI.WebControls.ListBox();
                //lb.Visible = true;
                //ListItem lm = new ListItem();
                //lm.Text = "nevzat kaya";
                //lb.Items.Add(lm);

                //string query2 = "Select categoryname,categoryid from categories where parentid is NULL";
                //Database db2 = new Database();
                //DataTable dt2 = db2.select(query2);


                //lb.ID = "1";

                //lb.DataTextField = "categoryname";
                //lb.DataValueField = "categoryid";
                //lb.DataSource = dt2;
                //lb.DataBind();
                //lb.AutoPostBack = true;
                //Panel1.Controls.Add(lb);
                //((ListBox)Panel1.FindControl(lb.ID)).SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
                
          
            //if (Convert.ToInt32(Session["count"]) !=0)
            //{
            //    ListBox lr = (ListBox)sender;
            //    string query1 = "Select categoryname,categoryid from categories where parentid=" + lr.SelectedValue + "";

            //    lb.ID = lb + c.ToString();
            //    Database db2 = new Database();
            //    DataTable dt2 = db2.select(query1);
            //    lb.DataTextField = "categoryname";
            //    lb.DataValueField = "categoryid";
            //    lb.DataSource = dt2;
            //    lb.AutoPostBack = true;
            //    lb.DataBind();
            //    Panel1.Controls.Add(lb);
            //    c++;  
            //    ((ListBox)Panel1.FindControl(lb.ID)).SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);

            //}
           

        }
        protected void Degistir_SelectedIndexChanged(ListBox lr, int a)
        {
            ListBox list = new ListBox();
            list.ID = "list" + c.ToString();
            string query = "Select categoryname,categoryid from categories where parentid=" + a + "";

            Database db = new Database();
            DataTable dt = db.select(query);
            list.DataTextField = "categoryname";
            list.DataValueField = "categoryid";
            list.DataSource = dt;
            list.AutoPostBack = true;
            list.DataBind();
            Panel1.Controls.Add(list);
            c++;
            //list.SelectedIndexChanged += new EventHandler(Degistir_SelectedIndexChanged(list, Convert.ToInt32(list.SelectedValue)));

        }
     
        protected void  ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //////////////////////////deneme
           // Session["count"] = c.ToString();
            ListBox lb = new ListBox();

            if(Session["count"]==null)
              (Session["count"])="0";
          for (int i = 0; i < Convert.ToInt32(Session["count"]); i++)
          {
              //if (i == 0)
              //{

              //    ////
              //    string query3 = "Select categoryname,categoryid from categories where parentid is NULL";
              //    Database db3 = new Database();
              //    DataTable dt3 = db3.select(query3);


              //    lb.ID = i.ToString();

              //    lb.DataTextField = "categoryname";
              //    lb.DataValueField = "categoryid";
              //    lb.DataSource = dt3;
              //    lb.DataBind();
              //    lb.AutoPostBack = true;
              //    Panel1.Controls.Add(lb);
              //    Panel1.DataBind();
              //    ((ListBox)Panel1.FindControl(lb.ID)).SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
              //    ////
              //}
              string query2 = "Select categoryname,categoryid from categories where parentid=" + ((ListBox)Panel1.FindControl((i).ToString())).SelectedValue + "";

              Database db2 = new Database();
              DataTable dt2 = db2.select(query2);
              lb.ID = (i + 1).ToString();
              lb.DataTextField = "categoryname";
              lb.DataValueField = "categoryid";
              lb.DataSource = dt2;
              lb.AutoPostBack = true;
              lb.DataBind();
              d++;
              Panel1.Controls.Add(lb);
              ((ListBox)Panel1.FindControl(lb.ID)).SelectedIndexChanged += new EventHandler(Page_Load);
              c++;
              Session["count"] = c.ToString();
          }
            ////////////////////////
            //////c=Convert.ToInt32(Session["count"]);
            ////// c++;
            //////    ListBox lr = (ListBox)sender;
            //////    string query = "Select categoryname,categoryid from categories where parentid=" + lr.SelectedValue + "";
                
            //////    ListBox lb = new ListBox();
            //////    lb.ID = c.ToString();
            //////    //lb.ID = "list" + c.ToString();
            //////    Database db = new Database();
            //////    DataTable dt = db.select(query);
            //////    lb.DataTextField= "categoryname";
            //////    lb.DataValueField = "categoryid";
            //////    lb.DataSource = dt;
            //////    lb.DataBind();
            //////    lb.AutoPostBack = true;
          
            //////    Panel1.Controls.Add(lb);
              
               
            //////    Session["count"] = c.ToString();
            //////    ((ListBox)Panel1.FindControl(lb.ID)).SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
         
            
           // lb.SelectedIndexChanged += new EventHandler(degistir(lb, Convert.ToInt32(lb.SelectedValue)));

        }

        void lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lr = (ListBox)sender;
            ListBox list = new ListBox();
            list.ID = list + c.ToString();
            string query = "Select categoryname,categoryid from categories where parentid=" + lr.SelectedValue + "";

            Database db = new Database();
            DataTable dt = db.select(query);
            list.DataTextField = "categoryname";
            list.DataValueField = "categoryid";
            list.DataSource = dt;
            list.AutoPostBack = true;
            list.DataBind();
            Panel1.Controls.Add(list);
            c++;
            list.SelectedIndexChanged +=new EventHandler(lb_SelectedIndexChanged);


        }

        

      
    }
}
